<?php
/*
超级管理，管理登陆本系统的账号
*/
class AdminController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;

	public function init() {

		$this->commonAction();	

	}
	//用户首页
	public function indexAction() {
		try {
			 
			$sql='select * from admin_central ';
			$result=$this->_dbAdapter ->fetchAssoc ( $sql );
			//print_r($result);exit;
			foreach($result as $key => $value){
				$result[$key]['action']=explode(',',$value['action_list']);
				$result[$key]['lasttime']=date('Y-m-d H:i',$value['lasttime']);
				$result[$key]['id']=$value['id'];
			}
		 
			$this->view->result=$result;
			$this->view->nickname=$this->_user->nickname;
			//$this->view->actionlist=explode(',',$this->setting['actionlist']);
		
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//删除用户
	public function deleteAction() {
		try {
			//获取用户id
			$id = ( int ) $this->_request->getParam ( 'id' );
			if (! $id){
				throw new Exception ( 'parameter_error' );
			}else{
				$res=$this->_dbAdapter->delete('admin_central', 'id='.$id);
				if($res)
					$this->feedback ( '删除成功', '注意', '/'.$this->controller, 'warning' );
				else 
					throw new Exception ( '删除失败 ！' );	
			}
				
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

	//增加新用户
	public function opaddAction(){
		try{
			if(! $this->isPost ()){
				throw new Exception ( '参数错误！' );
			}
			$p=$this->_request->getPost();
			
			if (empty ( $p['account'])) {
				throw new Exception ( '帐号不能为空！' );
			}
			if (empty ( $p['nickname'])) {
				throw new Exception ( '昵称不能为空！' );
			}
			if (empty ( $p['password'] )) {
				throw new Exception ( '密码不能为空！' );
			}
			if ($p['repassword'] != $p['repassword']) {
				throw new Exception ( '两次密码不一致！' );
			}
			
			$p['password']=$this->authcode($p['password'],'','mactop');
			$sql="select * from admin_central where username='".$p['account']."'";
			$result=$this->_dbAdapter->fetchAll($sql);
			if($result){
				throw new Exception ( '帐号已存在！' );
			}else{
				$lasttime=time();
				$data=array(
					'username'=>$p['account'],
					'nickname'=>$p['nickname'],
					'password'=>$p['password'],
					'lasttime'=>$lasttime
				);
				$res=$this->_dbAdapter->Insert('admin_central',$data);
				
				if( $res )
					$this->feedback ( '增加成功', '注意', '/'.$this->controller, 'warning' );
				else 
					throw new Exception ( '增加失败 ！' );	
			}
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
		
		//用户首页
	public function opAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			
			$p =$this->_request->getPost ();
			$isok=0;
			foreach($p['username'] as $key => $value){
				if($key==1)
					continue;
					
				$data=array('username'=>$value);
				if(!empty($p['password'][$key]))
					$data['password']=md5($p['password'][$key]);
				
				if(!empty($p['actionlist'][$key]))
					$data['action_list']=implode(',',$p['actionlist'][$key]);
					
				$result=$this->_dbAdapter ->update ( 'admin_central',$data,'id='.$key );
				
				if($result)
					$isok=1;	
			}
			if($isok)
				$this->feedback ( '更新成功', '注意', '/'.$this->controller, 'warning' );
			else	
				throw new Exception ( '更新失败 ！' );
			//print_r($p);exit;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
		//用户首页
	public function infoAction() {
		try {
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

		//用户首页
	public function opinfoAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			
			$p =$this->_request->getPost ();
           
			if (empty ( $p['oldpassword'] )) {
				throw new Exception ( '旧密码不能为空！' );
			}
			
			if (empty ( $p['newpassword'])) {
				throw new Exception ( '新密码不能为空！' );
			}
			
			if ($p['repassword'] != $p['repassword']) {
				throw new Exception ( '两次密码不一致！' );
			}
						
		   //	$p['oldpassword']=$this->authcode($p['oldpassword'],'','mactop');
			$sql = 'select password from admin_central where id=' .$this->_user ->id;
			$password=$this->_dbAdapter ->fetchOne ( $sql );
			 
			$password= $this->authcode($password,'DECODE','mactop');
			 
			if( $password != $p['oldpassword'] ){
				throw new Exception ( '旧密码错误！' );
			}else{	
				$p['newpassword']=$this->authcode($p['newpassword'],'','mactop');
				//echo $p['newpassword'];exit;
				$result=$this->_dbAdapter ->update ('admin_central',array('password'=>$p['newpassword']),'id='. $this->_user ->id);
			}
			if($result)
				$this->feedback ( '更新成功', '注意', '/login/out', 'warning' );
			else 
				throw new Exception ( '更新失败 ！' );	

			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function jtadminAction() {
		try {
			$groupname = $this->_request->getParam ( 'groupname' );
			$where='';
			if ( $groupname){
				$where.=' and groupname like "%'.$groupname.'%" or groupname_en like "%'.$groupname.'%" ';
			}
			
            $count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
    		$offset = ($curpage - 1) * $count; 

			$sql="SELECT SQL_CALC_FOUND_ROWS groupid,groupname,groupname_en FROM `groups` where 1 ".$where." limit $offset,$count";
			$result=$this->_dbAdapter ->fetchAssoc ( $sql );
    		$param ['file'] = '/' . $this->controller . '/' . $this->action;
    		$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    		$param ['perpagenum'] = $count; // 每页显示的数目
    		$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    		$param ['curpage'] = $curpage; // 当前页码
			$this->view->allnum=$param ['totalnum'];

    		$page = new MyClass_Page ( $param );
    		$page->setvar (array());
			$this->view->page= $page->getNumPage (); 
			$str=implode(',',array_keys($result));
			$sql="SELECT groupid,adminid,adminname,password,password_code FROM `admin` where schoolid=0 and groupid in (".$str.")";
			$admin=$this->_dbAdapter ->fetchAssoc ( $sql );
			//print_r($admin);exit;
			
			
			$this->view->result=$result;
			$this->view->groupname=$groupname;
			$this->view->admin=$admin;
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
    public function resetpasswordAction() {
		try { 
         $id = ( int ) $this->_request->getParam ( 'id' ); 

		 $data=array();
		 $data['password']=md5('111111') ; 
		 $data['password_code']='111111' ; 
		 
 		 $result = $this->_dbAdapter->update ('admin',$data,'adminid='.$id);
		 if($result){ 
		    $this->feedback ( '修改成功','成功','/admin/jtadmin/id/'.$id,'tip',3 ); 
		  }else{ 
			 $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' ); 	 
				 } 
          
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
    public function resetcentralpasswordAction() {
		try { 
		 $id = ( int ) $this->_request->getParam ( 'id' ); 
		 $p['password']='111111' ; 
		 $p['password']=$this->authcode($p['password'],'','mactop');
		 
 		 $result = $this->_dbAdapter->update ('admin_central',array('password'=>$p['password']),'id='.$id);
		 if($result){ 
		    $this->feedback ( '修改成功','成功','/admin/index','tip',3 ); 
		 }else{ 
			 $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' ); 	 
		 } 
          
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	public function xxadminAction() {
		try {

			$groupid = ( int ) $this->_request->getParam ( 'groupid' );
			$keyword = $this->_request->getParam ( 'keyword' );
			//print_r($groupid);print_r($keyword);exit;
			$where='';
			if ( $keyword){
				$where.=' and schoolname like "%'.$keyword.'%" ';
			}
			$where_group='';
			if ( $groupid){
				$where_group.=' and groupid ='.$groupid;
			}
			$this->view->groupid=$groupid;
			$this->view->keyword=$keyword;
			
			$sql="SELECT groupid,groupname,groupname_en FROM `groups` where 1 ".$where_group;
			$group=$this->_dbAdapter ->fetchAssoc ( $sql );
			$this->view->group=$group;
			
			$sql='select schoolid,schoolname,groupid from school where 1 '.$where_group.$where;
			$schoolname=$this->_dbAdapter ->fetchAssoc ( $sql );
			$this->view->school_name=$schoolname;

            $count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
    		$offset = ($curpage - 1) * $count; 
			$sql="SELECT SQL_CALC_FOUND_ROWS schoolid,adminid,adminname,groupid,password,password_code FROM `admin` where schoolid<>0 ".$where_group." limit $offset,$count";
			$school=$this->_dbAdapter ->fetchAssoc ( $sql );
    		$param ['file'] = '/' . $this->controller . '/' . $this->action;
    		$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    		$param ['perpagenum'] = $count; // 每页显示的数目
    		$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    		$param ['curpage'] = $curpage; // 当前页码
			$this->view->allnum=$param ['totalnum'];

    		$page = new MyClass_Page ( $param );
    		$page->setvar (array());
			$this->view->page= $page->getNumPage (); 
			$this->view->school=$school;
			
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
    public function setupAction() {
		try { 
		 $id = ( int ) $this->_request->getParam ( 'id' ); 
		 if(!$id){
		 	throw new Exception ( '参数错误！' );	
		 }
		 $this->view->id=$id;
		 $sql='SELECT menuid,menu FROM `menu` where ishidden=0 and istopbar=1';
		 $menu=$this->_dbAdapter ->fetchAssoc ( $sql );
		 
		 $sql='SELECT action_list FROM `admin_central` where id='.$id;
		 $action_list=$this->_dbAdapter ->fetchOne ( $sql );
		 $action=explode(',',$action_list);
		 
		 foreach($action as $key=>$value){
		 	if (in_array($value, array_keys($menu))) {
				$menu[$value]['ischecked']=1;
			}
		 }
		 $this->view->menu=$menu;
		 //print_r($menu);exit;
 		 
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	public function opsetupAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			
			$p =$this->_request->getPost ();
            //print_r($p);exit;
			if (empty ( $p['id'] )) {
				throw new Exception ( '参数错误！' );
			}
			
			if (empty ( $p['menuid'])) {
				throw new Exception ( '请选择权限！' );
			}
			
			$action_list=implode(',',array_values($p['menuid']));
			//print_r($action_list);exit;	
			$data=array('action_list'=>$action_list);
			//print_r($data);exit;
 		 	$result = $this->_dbAdapter->update ('admin_central',$data,'id='.$p['id']);

			if($result)
				$this->feedback ( '更新成功', '注意', '/admin/index', 'warning' );
			else 
				throw new Exception ( '更新失败 ！' );	

			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}}

